package net.xdclass.onlinexdclass.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.xdclass.onlinexdclass.model.entity.VideoOrder;
import net.xdclass.onlinexdclass.model.request.VideoOrderRequest;
import net.xdclass.onlinexdclass.service.VideoOrderService;
import net.xdclass.onlinexdclass.utils.JsonDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api("视频订单")
@RestController
@RequestMapping("/api/v1/pri/order")
public class VideoOrderController {

    @Autowired
    private VideoOrderService videoOrderService;


    @ApiOperation(value = "下单接口")
    @PostMapping("/save")
    public JsonDate saveOrder(@RequestBody VideoOrderRequest videoOrderRequest, HttpServletRequest request) {
        Integer userId = (Integer) request.getAttribute("user_id");

        Integer integer = videoOrderService.save(userId, videoOrderRequest.getVideoId());

        return integer == null?JsonDate.bulidError("下单失败"):JsonDate.bulidSuccess("下单成功");
    }



    @ApiOperation(value = "订单列表接口开发")
    @GetMapping("/list")
    public JsonDate listOrder(HttpServletRequest request) {
        Integer user_id = (Integer) request.getAttribute("user_id");

        List<VideoOrder> videoOrderList = videoOrderService.listOrderByUserId(user_id);

        return JsonDate.bulidSuccess(videoOrderList);
    }
}
